﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace DependencyInjection
{
    public abstract class AbstractModule
    {
        /// <summary>
        /// Maps the interface type to the implementation
        /// </summary>
        private Dictionary<Type, Type> _mapTypes = new Dictionary<Type, Type>();

        private Dictionary<Type, bool> _mapSingletons = new Dictionary<Type, bool>();

        public AbstractModule()
        {
            Configure();
        }

        protected abstract void Configure();

        protected void Bind(Type tInterface, Type tImplementation, bool singleton)
        {
            //if (tInterface.IsAssignableFrom(tImplementation))
            {
                if (!_mapTypes.ContainsKey(tInterface))
                {
                    _mapTypes.Add(tInterface, tImplementation);
                    _mapSingletons.Add(tInterface, singleton);

                    if (tInterface != tImplementation)
                    {
                        _mapSingletons.Add(tImplementation, singleton);
                    }
                }
            }
        }

        public Type Get(Type type)
        {
            Type result = null;

            if (_mapTypes.ContainsKey(type))
            {
                result = _mapTypes[type];
            }
            else
            {
                result =  type;
            }

            return result;
        }

        public bool IsSingle(Type type)
        {
            bool result = false;

            if (_mapSingletons.ContainsKey(type))
            {
                result = _mapSingletons[type];
            }

            return result;
        }
    }
}
